package com.tjp.eight.ui.fragment.jifenshop.goodsdetail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.SharePreferencesUtils;

/**
 * Created by Administrator on 2017/4/11.
 */
public class FragmentGoodsDetail extends BaseFragment<GoodsPresenter> implements GoodsDetailView {
    private WebView goods_detail;
    private TextView tvDes;
    private TextView tvNeedMoney;
    private TextView tvSub;
    ImageView iv_goods_thumb;
    private String gid;

    @Override
    protected GoodsPresenter createPresenter() {
        return new GoodsPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_goods_detail, container, false);
        }
        initViews();
        gid = getActivity().getIntent().getStringExtra("gid");
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loadData(getPJson());
    }

    private String getPJson() {
        GoodsDetailPModel model = new GoodsDetailPModel();
        model.gid = gid;
        return new Gson().toJson(model);
    }

    void initViews() {
        goods_detail = (WebView) contentView.findViewById(R.id.wv_goods_detail);
        tvDes = (TextView) contentView.findViewById(R.id.tv_goods_des);
        tvNeedMoney = (TextView) contentView.findViewById(R.id.tv_need_money);
        tvSub = (TextView) contentView.findViewById(R.id.tv_dui_huan);
        iv_goods_thumb = (ImageView) contentView.findViewById(R.id.iv_goods_thumb);

        tvSub.setEnabled(false);
        tvSub.setText("积分不足");
        tvNeedMoney.setText("0");
        tvDes.setText("");
        setWebView(goods_detail);
    }

    void setWebView(WebView view) {
        view.getSettings().setSupportZoom(false);
        view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        view.getSettings().setJavaScriptEnabled(true);
        view.getSettings().setDefaultTextEncodingName("UTF-8");
        view.getSettings().setAppCacheEnabled(false);
        view.getSettings().setSaveFormData(true);// 保存表单数据
        view.getSettings().setJavaScriptEnabled(true);
        view.getSettings().setGeolocationEnabled(true);// 启用地理定位
        view.getSettings().setDomStorageEnabled(true);
        view.getSettings().setSupportMultipleWindows(true);// 新加
        switch (SharePreferencesUtils.geWebTextSize()) {
            case 2:
                view.getSettings().setTextZoom(75);
                break;
            case 3:
                view.getSettings().setTextZoom(100);
                break;
            case 4:
                view.getSettings().setTextZoom(150);
                break;
            default:
                view.getSettings().setTextZoom(100);
                break;
        }
    }

    @Override
    public void loadSuccess(GoodsDetailRModel model) {
        if (model != null && model.data != null) {
            tvNeedMoney.setText("" + model.data.money);
            Glide.with(getContext()).load(model.data.thumb).error(R.drawable.icon_fail).into(iv_goods_thumb);
            tvDes.setText("" + model.data.des);
            int need = Integer.parseInt(model.data.money);
            int has = Integer.parseInt(SharePreferencesUtils.getMoney());

            if (has >= need) {
                tvSub.setText("立即兑换");
                tvSub.setEnabled(true);
            }
            goods_detail.loadData(model.data.detail,"text/html","utf-8");
        }
    }

    @Override
    public void fail(String msg) {

    }
}
